package com.earl.spider.config;

import com.earl.spider.util.FilePath;
import com.earl.spider.util.FileUtil;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class ChromeConfig {
    public static boolean isCopyUserData = false;
    private static final Logger logger = LoggerFactory.getLogger(ChromeConfig.class);


    public static WebDriver isCopyInit() {
        if (ChromeConfig.isCopyUserData) {
            return ChromeConfig.init(false);
        } else {
            return ChromeConfig.init();
        }
    }

    /**
     * 谷歌浏览器初始化
     * <p>
     * 加载用户默认的配置文件 false 不复制用户默认配置文件
     *
     * @return
     */
    public static WebDriver init() {
        WebDriver init = init(true, false, true, false);
        isCopyUserData = true;
        return init;
    }

    public static WebDriver init(boolean isDefault) {
        return init(isDefault, false, true, false);
    }

    public static WebDriver init(boolean isDefault, boolean isTranslate) {
        return init(isDefault, isTranslate, true, false);
    }

    /**
     * 谷歌浏览器初始化
     *
     * @param isDefault true 加载用户默认的配置文件 false 不复制用户默认配置文件
     * @return
     */
    public static WebDriver init(boolean isDefault, boolean isTranslate, boolean userData, boolean ignoreCssImage) {
        //初始化目录
        File file = new File(FilePath.driverPath2);
        if (file.exists()) {
            System.setProperty("webdriver.chrome.driver", FilePath.driverPath2);
        } else {
            System.setProperty("webdriver.chrome.driver", FilePath.driverPath);
        }
        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1);
        chromePrefs.put("download.prompt_for_download", false);
        chromePrefs.put("download.default_directory", FilePath.chromeDownPath);
        ChromeOptions options = new ChromeOptions();
        // 全屏
        options.addArguments("start-maximized");
        // 去掉提示“chrome正受到自动化测试软件的控制”
        options.addArguments("disable-infobars");
        //打开默认的
        if (isDefault) {
            logger.info("复制谷歌配置文件开始");
            if (userData) FileUtil.createUserData(FilePath.bat);
            logger.info("复制谷歌配置文件结束");
            options.addArguments("user-data-dir=" + FilePath.spiderChromPath);
        }
        if (isTranslate) {
            Map<String, Object> langs = new HashMap<>();
            langs.put("ja", "en");
            chromePrefs.put("translate", "{'enabled' : true}");
            chromePrefs.put("translate_whitelists", langs);
        }
        // 打开默认的
        if (ignoreCssImage) {
            chromePrefs.put("profile.managed_default_content_settings.images", 2);
            chromePrefs.put("permissions.default.stylesheet", 2);
        }
        options.setExperimentalOption("prefs", chromePrefs);
        options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        cap.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        FileUtil.deleteAll(FilePath.chromeDownPath);
        FileUtil.deleteAll(FilePath.chromeDownToFtp);
        return driver;
    }
}
